package com.suredy.app.project.ctrl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suredy.app.project.constants.ProjectStatus;
import com.suredy.app.project.service.ProjectSrv;
import com.suredy.core.ctrl.BaseCtrl;

/**
 * 首页
 * 
 * @author VIVID.G
 * @since 2015-9-7
 * @version v0.1
 */
@Controller
@RequestMapping("/front-page")
public class FrontPageCtrl extends BaseCtrl {

	@Autowired
	private ProjectSrv projectSrv;

	@RequestMapping(method = RequestMethod.GET)
	public String index() {
		String view = "app/project/front-page";

		return view;
	}

	/**
	 * 年度产值
	 * 
	 * @param year
	 * @return
	 */
	@RequestMapping(value = "/this-year-output-value/{year}", method = RequestMethod.POST)
	@ResponseBody
	public Object thisYearOutputValue(@PathVariable int year) {
		Map<String, Float> ratio = this.projectSrv.getProjectOutputValue(year);

		if (ratio == null || ratio.isEmpty())
			return null;

		Map<String, List<Object>> ret = new HashMap<String, List<Object>>();
		ret.put("xData", new ArrayList<Object>());
		ret.put("yData", new ArrayList<Object>());

		List<Object> xData = ret.get("xData");
		xData.add("合同金额");
		xData.add("结算金额");
		xData.add("收款金额");
		xData.add("支出金额");

		List<Object> yData = ret.get("yData");
		yData.add(ratio.get("price"));
		yData.add(ratio.get("payPrice"));
		yData.add(ratio.get("payIn"));
		yData.add(ratio.get("payOut"));

		return ret;
	}

	@RequestMapping(value = "/this-year-project-perform-status/{year}", method = RequestMethod.POST)
	@ResponseBody
	public Object thisYearProjectPerformStatus(@PathVariable int year) {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, Calendar.JANUARY);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);

		Date start = c.getTime();

		c.add(Calendar.YEAR, 1);

		Date end = c.getTime();

		Map<ProjectStatus, Integer> ratio = this.projectSrv.getProjectStatusCount(start, end);

		if (ratio == null || ratio.isEmpty())
			return null;

		Map<String, List<Object>> ret = new HashMap<String, List<Object>>();
		ret.put("xData", new ArrayList<Object>());
		ret.put("yData", new ArrayList<Object>());

		for (Map.Entry<ProjectStatus, Integer> entry : ratio.entrySet()) {
			ProjectStatus status = entry.getKey();
			Integer count = entry.getValue();

			switch (status) {
				case COMPLETED:
					ret.get("xData").add("完成");
					break;
				case NOT_START:
					ret.get("xData").add("未实施");
					break;
				case STARTED:
					ret.get("xData").add("在建");
					break;
				case SUSPEND:
					ret.get("xData").add("中止");
					break;
				default:
					ret.get("xData").add("未知");
					break;
			}

			ret.get("yData").add(count);
		}

		return ret;
	}

}
